<?php
namespace App\Http\Controllers\Alipay;
use App\Enums\HelpCenterStatusEnum;
use App\Models\Article;
use App\Models\ArticleType;
use App\Models\HelpCategory;
use App\Models\HelpCenter;
use App\Models\OtherWithholdLog;
use App\Models\Help;
use App\Services\AlipayService;
use App\Services\CommonService;
use Illuminate\Http\Request;

//关于我们、帮助中心
class HelpController extends BaseVerifyController
{
    //获取关于我们分类 http://shop.cn/Alipay/Help/getAboutUsType?type_name=关于我们
    public function getAboutUsType(Request $request)
    {
        /*$orderData = [
            'orderId'=>'20220707173907716678',
            'outTradeNo'=>'20220707173907716678|141',
            'tradeNo'=>'2022070722001476081442888051',
            'status'=>'COMPLETE',
        ];
        $orderInfo = AlipayService::orderInfo($orderData);
        print_r($orderInfo);die;*/

        $input = $request->input();
        if (empty($input['type_name'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $first = ArticleType::GetOneArticleType(array('type_name' => $input['type_name'], 'status' => ArticleType::ARTICLE_TYPE_STATUS_DISPLAY, 'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
        if (empty($first)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '信息不存在');
        }
        $second = ArticleType::GetSecondCategory(array('parentid' => $first['id'], 'status' => ArticleType::ARTICLE_TYPE_STATUS_DISPLAY, 'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $second);
    }

    //获取关于我们 http://shop.cn/Alipay/Help/getAboutUs?type_id=
    public function getAboutUs(Request $request)
    {
        $input = $request->input();
        if (empty($input['type_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $data = Article::select('id', 'type_id', 'parentid', 'article_title', 'content')
            ->where(array('type_id' => $input['type_id'], 'xcx_show' => Article::ARTICLE_XCX_IS_SHOW, 'status' => Article::ARTICLE_STATUS_IS_RELEASE, 'is_delete' => Article::ARTICLE_NOT_DELETE))
            ->first();
        $data = $data ? $data->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取常见问题分类 http://shop.cn/Alipay/Help/getCommonProblemType?type_name=常见问题
    public function getCommonProblemType(Request $request)
    {
        $articleType = ArticleType::GetOneArticleType(array('id' => 10,
            'status' => ArticleType::ARTICLE_TYPE_STATUS_DISPLAY, 'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
        if (empty($articleType)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '信息不存在');
        }
        $data = Article::select('id', 'type_id', 'parentid', 'article_title')
            ->where(array('type_id' => $articleType['id'], 'xcx_show' => Article::ARTICLE_XCX_IS_SHOW, 'status' => Article::ARTICLE_STATUS_IS_RELEASE, 'is_delete' => Article::ARTICLE_NOT_DELETE))
            ->get();
        $data = $data ? $data->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取常见问题 http://shop.cn/Alipay/Help/getCommonProblemById?id=
    public function getCommonProblemById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $data = Article::select('id', 'type_id', 'parentid', 'article_title', 'content')
            ->where(array('id' => $input['id'], 'xcx_show' => Article::ARTICLE_XCX_IS_SHOW, 'status' => Article::ARTICLE_STATUS_IS_RELEASE, 'is_delete' => Article::ARTICLE_NOT_DELETE))
            ->first();
        $data = $data ? $data->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 帮助中心分类
     * @param Request $request
     * @return void
     */
    public function getHelpCategory(Request $request)
    {
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', HelpCategory::getAll());

    }

    public function getHelpList(Request $request)
    {
        $category_id = $request->input('category_id', 1);
        $list= HelpCenter::query()
            ->where('category_id',$category_id)
            ->where('status',HelpCenterStatusEnum::ON_SHELF()->getKey())
            ->with('category')
            ->select('id','title','status','sort','category_id')
            ->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->get();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功',$list);
    }

    public function getHelpDetail(Request $request)
    {

        $id = $request->input('id');
        $detail = HelpCenter::query()
            ->where('id',$id)
            ->where('status',HelpCenterStatusEnum::ON_SHELF()->getKey())
            ->select('id','title','status','note')
            ->first();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功',$detail);
    }

    /**
     * 平台公告列表
     */
    public function platformArticleList()
    {
        $data=Article::query()->where('type_id',21)->where('parentid',20)->where('status',1)
            ->where('is_delete',2)->select('id','article_title','update_time')->get();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    public function platformArticleDetail(Request $request)
    {
        $id=$request->input('id');
        $data=Article::query()->where('id',$id)->where('status',1)
            ->where('is_delete',2)->select('id','article_title','update_time','content')
            ->first();
        $data['content']=htmlspecialchars_decode($data['content']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 获取服务协议
     */
    public function getAgreement(){
        $agreement = Help::where('id',25)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }

    /**
     * 获取服务协议
     */
    public function getPrivacy(){
        $agreement = Help::where('id',26)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }

}
